package demo4;

import java.util.Arrays;

public class Test {
    public static void main(String[] args) {
        StringBuilder stringBuilder = new StringBuilder("abc");
        System.out.println(stringBuilder); // abc
        //字符串逆置
        stringBuilder.reverse();
        System.out.println(stringBuilder); //cba
        stringBuilder.append(324).append("dfgdfg");
        System.out.println(stringBuilder); // 此时 不会产生临时对象
        //将StringBuilder 转变为 String 类型
        String ret = stringBuilder.toString();

    }
    public static void main15(String[] args) {
        //字符串拼接
        String str = "hello";
        str += "asdf";
        System.out.println(str);//helloasdf
    }
    public static void main14(String[] args) {
        String str = "  asd fgh rt ";
        System.out.println(str);//  asd fgh rt
        //删除字符串左右两边的空格
        String ret = str.trim();
        System.out.println(ret);//asd fgh rt
    }
    public static void main13(String[] args) {
        //截取字符串
        String str = "abcdefgh";
        //返回从2位置 截取的所有字符
        String s = str.substring(2);
        System.out.println(s);
        String s1 = str.substring(2,6); // [ ) 前闭后开
        System.out.println(s1);
    }
    public static void main12(String[] args) {
        String str = "name=zhangsan&age=18";
        String[] strs = str.split("&");
        for (int i = 0; i < strs.length; i++) {
            String[] s = strs[i].split("=");
            for(String s1 : s) {
                System.out.println(s1);
            }
        }
    }
    public static void main11(String[] args) {
        String str1 = "192.168.1.1";
        String[] strs = str1.split("\\.");
        for (int i = 0; i < strs.length; i++) {
            System.out.println(strs[i]);
        }
        String str2 = "192\\168\\1\\1";
        String[] strs1 = str2.split("\\\\");
        for (int i = 0; i < strs1.length; i++) {
            System.out.println(strs1[i]);
        }
        String str3 = "192&123|168+11.1";
        String[] s = str3.split("&|\\+|\\.");
        for (int i = 0; i < s.length; i++) {
            System.out.println(s[i]);
        }
    }
    public static void main10(String[] args) {
        //字符串的拆分
        String str1 = "hello little bitter";
        //以空格进行拆分   分两组
        String[] strs = str1.split(" ",2);
        for (int i = 0; i < strs.length; i++) {
            System.out.println(strs[i]);
            //hello
            //little  bitter
        }
    }
    public static void main9(String[] args) {
        //字符串替换
        String str = "abcdabcdabcde";

        String ret = str.replace('a','p');
        System.out.println(ret); //  pbcdpbcdpbcde

        String ret2 = str.replace("ab","ooooo");
        System.out.println(ret2);  //  ooooocdooooocdooooocde

        //替换全部字符串
        String ret3 = str.replaceAll("bc","op");
        System.out.println(ret3); //  aopdaopdaopde

        //替换第一个字符串
        String ret4 = str.replaceFirst("abcd","uiop");
        System.out.println(ret4); //  uiopabcdabcde

    }
    public static void main8(String[] args) {
        String str1 = "abcdef";
        char[] chars = str1.toCharArray();
        System.out.println(chars);
        int[] arr = {1,2,4};
        //数组输出
        System.out.println(Arrays.toString(chars)); //[a, b, c, d, e, f]
        System.out.println(Arrays.toString(arr));  // [1, 2, 4]
        //格式化输出
        String s = String.format("%d - %d - %d",2019,9,14);
        System.out.println(s);  //  2019 - 9 - 14
    }
    public static void main7(String[] args) {
        //大小写转换
        String str1 = "abcd";
        String str2 = "ABCD";
        System.out.println(str1.toUpperCase()); // ABCD
        System.out.println(str2.toLowerCase()); // abcd

    }
    public static void main6(String[] args) {
        String str = String.valueOf(19);
        System.out.println(str); // 19

        String str1 = "123";
        int a = Integer.parseInt(str1);
        double b= Double.parseDouble(str1);
        System.out.println(b); //  123.0
        System.out.println(a); //123
        Integer integer = 12;
        System.out.println(integer.intValue());  // 12

    }
    public static void main5(String[] args) {
        String str1 = "helloe";
        char ch = str1.charAt(0);
        System.out.println(ch); // h
        //拿到字母第一次出现的位置
        System.out.println(str1.indexOf('e')); // 1
        System.out.println(str1.indexOf('e',3)); // 5
        System.out.println(str1.indexOf("el"));// 1
        System.out.println(str1.lastIndexOf('e')); // 5
        System.out.println(str1.lastIndexOf('e',3)); // 1

    }
    public static void main4(String[] args) {
        String str1 = new String("hello");
        String str2 = new String("hello");
        String str3 = new String("hello11");
        String str4 = new String("helloqwe");
        String str5 = new String("HELLO");

        System.out.println(str1.compareTo(str2)); // 相同返回 0
        //如果是长度不同则返回长度的差
        System.out.println(str2.compareTo(str3)); // -2
        System.out.println(str3.compareTo(str2)); // 2
        //若两个字符串str1 与 str4 它们的在str1的长度下是相同的  那么
        //就返回它们的差
        System.out.println(str1.compareTo(str4)); // -3
        //忽视大小写进行比较
        System.out.println(str1.compareToIgnoreCase(str5));
    }
    public static void main3(String[] args) {
        String str1 = new String("hello");
        String str2 = new String("hello");
        System.out.println(str1 == str2); //false  比较的是两个引用的地址
        System.out.println(str1.equals(str2)); //true
    }
    public static void main2(String[] args) {
        String str1 = "hello";
        System.out.println(str1.length()); //length()
        int[] array = new int[]{1,2,3,4};
        System.out.println(array.length); //数组求长度length后面没有()

        String str2 = "";
        System.out.println(str2.length());
        System.out.println(str2.isEmpty()); //这个空是 它所指向的对象里面一个字符都没有
        System.out.println("hello".length()); // 也可以这样写  5
        String str3 = null; //这个空是 它不指向任何对象
    }
    public static void main1(String[] args) {
        String str1 = "hello";
        String str2 = new String("hello");
        //字符数组
        char[] values = {'a','b','c','d'};
        String str3 = new String(values);
        System.out.println(str3); // abcd
    }
}
